<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      p {
        /* height: 300px; */
        border: solid;
      }
    </style>
    <style>
      li {
        border: solid;
        margin: 10px;
      }

      .selected {
        background: yellow;
      }
    </style>
  </head>
  <body>
    <ul id="list">
      <li data-item>CSS</li>
      <li data-item>HTML</li>
      <li data-item>JS</li>
    </ul>
    <script>
      const list = document.getElementById("list");
      list.onclick = function (e) {
        if (e.target.dataset.item === undefined) return;
        //在选中下一个item的时候，将之前的所有 selected 类清除
        if (e.ctrlKey === false) {
          // 当同时按下ctrl的时候多选
          list
            .querySelectorAll(".selected")
            .forEach((el) => el.classList.remove("selected"));
        }

        // 给点击选中的item添加高亮（单选）
        e.target.classList.add("selected");
      };
    </script>
  </body>
</html>
